﻿/*****************************************************************************//*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/

#include "os_obj.h"
#define _SYSTASK_MODULE_
#include "sys_task.h"
#undef _SYSTASK_MODULE_
#include "pincfg.h"
#include "sys_init.h"
#include "com_obj.h"
#include "common.h"
#include "bsp_hal.h"
#include "crc_ext.h"
#include "mbedtls/des.h"
#include "code_verify.h"
#include "uuid_verify.h"

void sys_led_ctr(void);
void system_info(char pos);
void sys_ui_tst(void);
void at_flash_test(void);
void des_demo(void);
void base64_demo(void);
void aes_demo(void);
void md5_demo(void);
void sha256_demo(void);
void rsa_demo(void);
void bit_map_demo(void);
void cvf_verify_check(void);
void uidvf_verify_check(void);

int core_main(int argc, char *argv[]);

char * listn[] = 
{
    [0] = {"new""new2"}, //编译器链接为一个字符串
    [1] = {"en"},
};

/*****************************************************************************
*
* @brief   system task.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_system(ULONG thread_input)
{
	(void)thread_input;

	uint32_t us_tick_s, us_tick_end;

	sys_init();

	system_info(0);
	
	des_demo();
	base64_demo();
	aes_demo();
	md5_demo();
	sha256_demo();
	// rsa_demo();
	// bit_map_demo();
	
    // at_flash_test();
    
	// cvf_handle();
	cvf_verify_check();
	//uidvf_handle();
	uidvf_verify_check();
	
	while (1)
	{
        bsp_dwt_check();
        
		us_tick_s = app_tick_us_get();

		sys_st.u_tick = app_tick_get();

		if (sys_st.b_coremark)
		{
			sys_st.b_coremark = 0;
		}
		
		pulse_create(&sys_st.pulse, sys_st.u_tick);
		if (sys_st.pulse.bPulse_1s)
		{
			TimeRun(&sys_st.rtc_time);
			O_LED1.dir_out_pp();
			O_LED1.tog();
		}
        
		sys_st.sig_time = time_to_sig(&sys_st.rtc_time);
		time_sig_to_time(sys_st.sig_time, &sys_st.rtc_time);
		crc16_modbus_calc(sys_st.ui.buff, 16);
		crc16_modbus_calc(sys_st.ui.buff, 16);
        
		sys_led_ctr();

		us_tick_end = app_tick_us_get();

		sys_st.exe_time_us = us_tick_end - us_tick_s;

        if(sys_st.exe_time_us_max < sys_st.exe_time_us)
        {
            sys_st.exe_time_us_max = sys_st.exe_time_us;
        }
        
		NOP1();
		osDelay(ms_ticks(10));
		NOP1();
	}
}

/*****************************************************************************
*
* @brief   led control.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_led_ctr(void)
{
	if (sys_st.pulse.bPulse_1s)
	{
		//O_LED1.dir_out_pp();
		//O_LED1.tog();
	}

	sys_st.led_obj.IncCoef = 50;
	sys_st.led_obj.min = 100;
	sys_st.led_obj.max = 900;
	sys_st.led_obj.max_keep_time_sv = 2;
	sys_st.led_obj.min_keep_time_sv = 2;
	led_breath_handle(&sys_st.led_obj);

	app_tick_duty_set(4, sys_st.led_obj.out);
}

/*****************************************************************************
*
* @brief   task switch hook.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_sw_hook(char pos)
{

}

/*****************************************************************************
*
* @brief   system info.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void system_info(char pos)
{
	RCC_ClockType RCC_Clocks;
	RCC_GetClocksFreq(&RCC_Clocks);

	dbg_puts("\n\n");
	dbg_puts("==========================================\n");
	dbg_print("compile time: %s  %s\n", __DATE__, __TIME__);
	dbg_puts("RTOS : ThreadX\n");
	dbg_print("AHB CLK: %dHz\n", RCC_Clocks.AHBCLK_Freq);
	dbg_print("APB1 CLK: %dHz\n", RCC_Clocks.APB1CLK_Freq);
	dbg_print("APB2 CLK: %dHz\n", RCC_Clocks.APB2CLK_Freq);
	dbg_print("ADC CLK: %dHz\n", RCC_Clocks.ADCCLK_Freq);
	dbg_puts("==========================================\n");
}

/*****************************************************************************
* @brief   task switch hook.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_pre_init(char pos)
{
	sys_st.ui.str_len = 12;
	sys_st.ui.ip_in = 0x72;
	sys_st.ui.min = INT32_MIN;
	sys_st.ui.max = INT32_MAX;

	sys_st.b_coremark = 0;
}
OBJ_INIT_APP_EXPORT(sys_pre_init);

/*****************************************************************************
* @brief   system ui test.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_ui_tst(void)
{
	if (sys_st.ui.ch_add)
	{
		ui_num_text_input_add(&sys_st.ui.obj, sys_st.ui.ch_add);
		sys_st.ui.ch_add = 0;
	}

	if (sys_st.ui.del_req)
	{
		ui_num_text_input_del(&sys_st.ui.obj);
		sys_st.ui.del_req = 0;
	}

	if (!sys_st.ui.rst_comp)
	{
		ui_text_input_reset(&sys_st.ui.obj, sys_st.ui.buff, sys_st.ui.str_len);
		ui_num_text_input_rang(&sys_st.ui.obj, 1, sys_st.ui.min, sys_st.ui.max);
		ui_num_text_input_inpn_set(&sys_st.ui.obj, sys_st.ui.ip_in >> 4, sys_st.ui.ip_in & 0xF);
		sys_st.ui.rst_comp = 1;
	}
}

/*******************************************************************************
* @brief  at_flash_test
* \param[in] none
* \retval: none
*******************************************************************************/
void at_flash_test(void)
{
    uint32_t tick_s;
    uint32_t cnt;
    
    FLASH_UnlockBank2();
    
    FLASH_ErasePage(0x8000000 + 512*1024);
    
    //填充全零擦除
    for(cnt=0; cnt<512; cnt++)
    {
        FLASH_ProgramWord(0x8000000 + 512*1024 + 4*cnt, 0);
    }
    
    tick_s = app_tick_us_get();
    
    FLASH_ErasePage(0x8000000 + 512*1024);
    
    dbg_print("Flash erase time with 0x00000000 : %8d us\n", app_tick_us_get() - tick_s);
    
    //填充一半零擦除
    for(cnt=0; cnt<512; cnt++)
    {
        FLASH_ProgramWord(0x8000000 + 512*1024 + 4*cnt, 0x0F0F0F0F);
    }
    
    tick_s = app_tick_us_get();
    
    FLASH_ErasePage(0x8000000 + 512*1024);
    
    dbg_print("Flash erase time with 0x0F0F0F0F : %8d us\n", app_tick_us_get() - tick_s);
    
    //全1擦除
    tick_s = app_tick_us_get();
    
    FLASH_ErasePage(0x8000000 + 512*1024);
    
    dbg_print("Flash erase time with 0xFFFFFFFF : %8d us\n", app_tick_us_get() - tick_s);
     
    
    FLASH_LockBank2(); 
}

uint32_t rt_tick_get(void)
{
	return sys_st.u_tick;
}

/*******************************************************************************
* @brief  lib surport
* \param[in] none
* \retval: none
*******************************************************************************/
#define LRC_ADDR01		0x20000000
#define LRC_ADDR02		0x20000004
#define LRC_ADDR03		0x20000008
#define LRC_ADDR04		0x2000000C
#define LRC_ADDR0_RES	0x20000010
#define LRC_ADDR0_NRES	0x20000014

void lib_os_lock(void)
{
	
}

void lib_os_unlock(void)
{

}

void lib_check_hook(int32_t st)
{
    uint32_t lrc;

	if(st > 2)
	{
		lrc = 0;
		lrc += inpw(LRC_ADDR01);
		lrc += inpw(LRC_ADDR02);
		lrc += inpw(LRC_ADDR03);
		lrc += inpw(LRC_ADDR04);
		
		outpw(LRC_ADDR0_RES, lrc );
		outpw(LRC_ADDR0_NRES, ~lrc);
	}
}

// 擦除加密代码并且尾部填充空指令
void code_jiami_end(void)
{

}